package Cn.edu.zafu.bigdata.cxy;

public class StaticDemo {
    // 静态变量
    static int count = 0;
    // 实例变量
    int id;

    // 静态初始化块
    static {
        System.out.println("Static block executed");
        count = 0; // 初始化静态变量count为0
    }

    // 实例初始化块
    {
        System.out.println("Instance block executed");
        this.id = count;
        count++;
    }

    // 构造函数
    StaticDemo() {
        System.out.println("Constructor executed for ID: " + this.id);
    }

    // 静态方法
    public static void displayCount() {
        System.out.println("Total instances created: " + count);
    }
}

class TestStaticDemo {
    public static void main(String[] args) {
        // 创建多个实例
        StaticDemo instance1 = new StaticDemo();
        StaticDemo instance2 = new StaticDemo();
        StaticDemo instance3 = new StaticDemo();

        // 调用静态方法，验证实例创建次数
        StaticDemo.displayCount();


    }
}

